package com.ruoyi.fjtseac.controller;

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.fjtseac.domain.dto.ApplicationFormDto;
import com.ruoyi.fjtseac.domain.dto.VolunteerDto;
import com.ruoyi.fjtseac.domain.po.ApplicationForm;
import com.ruoyi.fjtseac.domain.po.Volunteer;
import com.ruoyi.fjtseac.util.aop.RedisCacheEnable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fjtseac.service.VolunteerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 志愿者Controller
 *
 * @author icyrain11
 * @date 2023-06-29
 */
@RestController
@RequestMapping("/system/volunteer")
@Api(tags = {"志愿者管理接口"})
public class VolunteerController extends BaseController {
    @Autowired
    private VolunteerService volunteerService;

    /**
     * 查询志愿者管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:volunteer:list')")
    @GetMapping("/list")
    @ApiOperation("志愿者列表")
    public AjaxResult list(@Validated VolunteerDto volunteerDto) {
        return volunteerService.selectVolunteerPage(volunteerDto);
    }

    /**
     * 导出志愿者管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:volunteer:export')")
    @Log(title = "志愿者管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("志愿者信息导出")
    public void export(HttpServletResponse response, VolunteerDto volunteerDto) {
        List<Volunteer> list = volunteerService.selectVolunteerList(volunteerDto);
        ExcelUtil<Volunteer> util = new ExcelUtil<Volunteer>(Volunteer.class);
        util.exportExcel(response, list, "志愿者管理数据");
    }

    /**
     * 获取志愿者管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:volunteer:getInfo')")
    @GetMapping(value = "/{volunteerId}")
    @ApiOperation("获取志愿者管理详细信息")
    public AjaxResult getInfo(@PathVariable("volunteerId") Long volunteerId) {
        return AjaxResult.success(volunteerService.selectVolunteerByVolunteerId(volunteerId));
    }

    /**
     * 同意志愿者管理
     */
    @PreAuthorize("@ss.hasPermi('system:volunteer:agree')")
    @PostMapping(value = "/agree")
    @ApiOperation("同意志愿者管理")
    public AjaxResult agree(ApplicationFormDto applicationFormDto) {
        String createBy = getUsername();
        return volunteerService.agreeVolunteer(applicationFormDto, createBy);
    }

    /**
     * 新增志愿者管理
     */
    @PreAuthorize("@ss.hasPermi('system:volunteer:add')")
    @Log(title = "志愿者管理", businessType = BusinessType.INSERT)
    @ApiOperation("新增志愿者管理")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody VolunteerDto volunteerDto) {
        String createBy = getUsername();
        return volunteerService.insertVolunteer(volunteerDto, createBy);
    }

    /**
     * 修改志愿者管理
     */

    @PreAuthorize("@ss.hasPermi('system:volunteer:edit')")
    @Log(title = "志愿者管理", businessType = BusinessType.UPDATE)
    @ApiOperation("修改志愿者管理")
    @PutMapping
    public AjaxResult edit(@RequestBody VolunteerDto volunteerDto) {
        return toAjax(volunteerService.updateVolunteer(volunteerDto));
    }

    /**
     * 删除志愿者管理
     */
    @PreAuthorize("@ss.hasPermi('system:volunteer:remove')")
    @Log(title = "志愿者管理", businessType = BusinessType.DELETE)
    @ApiOperation("删除志愿者管理")
    @DeleteMapping("/{volunteerIds}")
    public AjaxResult remove(@PathVariable Long[] volunteerIds) {
        return toAjax(volunteerService.deleteVolunteerByVolunteerIds(volunteerIds));
    }

    /**
     * 导入志愿者文件
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:volunteer:import')")
    @Log(title = "志愿者管理", businessType = BusinessType.IMPORT)
    @ApiOperation("导入志愿者文件")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        String originalFileName = file.getOriginalFilename();
        String fileType = originalFileName.substring(originalFileName.lastIndexOf("."));

        if (fileType.equals(".xlsx") || fileType.equals("xls")) {//判断文件上是否为excel文件
            ExcelUtil<Volunteer> util = new ExcelUtil<Volunteer>(Volunteer.class);
            List<Volunteer> VolunteersList = util.importExcel(file.getInputStream());
            String operName = getUsername();
            String message = volunteerService.importVolunteer(VolunteersList, updateSupport, operName);
            return AjaxResult.success(message);
        }

        return AjaxResult.error("请上传excel文件");
    }

    /**
     * 导入志愿者模板
     *
     * @param response
     */
    @PreAuthorize("@ss.hasPermi('system:volunteer:template')")
    @ApiOperation("导出志愿者模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<VolunteerDto> util = new ExcelUtil<>(VolunteerDto.class);
        util.importTemplateExcel(response, "志愿者数据");
    }
}
